<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;

use app\admin\model\Incometype as IncometypeModel;

class Incometype extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $this->journals('查询', '查询收入类别', '');
        if (isset($data['ajax']) && $data['ajax'] == 100) {
            extract($data);
            $page = isset($data['page']) ? $data['page'] : 1;
            $page = intval($page);
            $limit = isset($data['rows']) ? $data['rows'] : 10;
            $limit = intval($limit);
            $start = $limit * ($page - 1);

            if (isset($name) && !empty($name)) {
                $list = Db::name('income_type')->where('name', 'like', '%' . $name . '%')->where('is_del', 0)->where('enterprise_id', $this->EnterpriseId)->order('id asc')->limit($start, $limit)->select();
                $count = Db::name('income_type')->where('name', 'like', '%' . $name . '%')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            } else {
                $list = Db::name('income_type')->where('is_del', 0)->order('id asc')->where('enterprise_id', $this->EnterpriseId)->limit($start, $limit)->select();
                $count = Db::name('income_type')->where('enterprise_id', $this->EnterpriseId)->where('is_del', 0)->count();
            }

            return $this->grid_data($list, $count, $page, $limit);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    //编辑栏目1-1
    public function edit($id)
    {
        $info = Db::name('income_type')->where('id', $id)->find();
        $this->assign('info', $info);
        return $this->fetch();
    }
    //编辑栏目1-2
    public function save()
    {
        $status = 0; //给$status赋值0
        $msg = "操作失败"; //$smg赋值操作失败
        $data = $this->request->param(); //获取传过来的所有数据
        $model = new IncometypeModel(); //实例化收入表模型
        if (empty($data['id'])) { //获取ID为空时添加有ID时更新
            $data['enterprise_id'] = $this->EnterpriseId; //给企业ID赋值
            $result = $model->allowField(true)->save($data); //给收入表添加数据
            if ($result) { //添加成功是输出的内容
                $this->journals('新增', '新增收入类别：' . $data['name'], ''); //给日志返回数据
                $status = 1;
                $msg = "添加成功";
            }
        } else { //有ID时返回的数据
            $id = $data['id']; //给收入表ID赋值
            $result = $model->allowField(true)->save($data, ["id" => $id]); //更新收入表对应ID的数据
            if ($result) { //更新数据成功时输出的数据
                $this->journals('更新', '更新收入类别：' . $data['name'], ''); //给日志返回数据
                $status = 1;
                $msg = "更新成功";
            }
        }
        return ['status' => $status, 'msg' => $msg];
    }

    //删除栏目1-1
    public function del($id)
    {
        $status = 0;
        $msg = "操作失败";
        $jn = Db::name('income_type')->where('id', $id)->find(); //获取到要删除的内容
        $this->journals('删除', '删除收入类别：' . $jn['name'], ''); //给日志返回数据
        $del = Db::name('income_type')->where('id', $id)->update(["is_del" => 1]);
        if ($del) {
            $status = 1;
            $msg = "删除成功";
        }
        return ['status' => $status, 'msg' => $msg];
    }
}
